package com.alone.embedded.lucene.spring;

import com.alone.embedded.lucene.core.EmbeddedLucene;
import com.alone.embedded.lucene.repository.handler.RepositoryHandler;
import com.alone.embedded.lucene.transaction.ElTransactionManager;
import com.alone.embedded.lucene.transaction.ElTransactionTemplate;
import org.apache.lucene.analysis.Analyzer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.Environment;
import org.springframework.lang.Nullable;

import java.util.List;
import java.util.Map;

/**
 * @author Alone
 * @date 2022/8/25 2:01 下午
 */
@EnableAspectJAutoProxy
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(EmbeddedLucene.class)
public class EmbeddedLuceneAutoConfiguration implements EnvironmentAware {

    private EmbeddedLuceneProperties properties;

    @Bean
    @ConditionalOnMissingBean(SpringEmbeddedLucene.class)
    public SpringEmbeddedLucene springEmbeddedLucene(List<RepositoryHandler> handlers,
                                                     Map<String, Analyzer> analyzers,
                                                     @Nullable FieldTypeAnalyzerMapper fieldTypeAnalyzerMapper) {
        return new SpringEmbeddedLucene(
                SpringEmbeddedLucene.buildConfig(
                        properties,
                        handlers,
                        fieldTypeAnalyzerMapper,
                        analyzers
                )
        );
    }

    @Bean
    @ConditionalOnBean(SpringEmbeddedLucene.class)
    public ElRepositoryRegistry elRepositoryRegistry(SpringEmbeddedLucene embeddedLucene) {
        return new ElRepositoryRegistry(embeddedLucene);
    }

    @Bean
    @ConditionalOnBean(SpringEmbeddedLucene.class)
    @ConditionalOnMissingBean(ElTransactionManager.class)
    public ElTransactionManager elTransactionManager(SpringEmbeddedLucene embeddedLucene) {
        return embeddedLucene.getTxManager();
    }

    @Bean
    @ConditionalOnBean(ElTransactionManager.class)
    @ConditionalOnMissingBean(ElTransactionTemplate.class)
    public ElTransactionTemplate elTransactionTemplate(ElTransactionManager elTransactionManager) {
        return new ElTransactionTemplate(elTransactionManager);
    }

    @Bean
    @ConditionalOnBean(ElTransactionTemplate.class)
    @ConditionalOnMissingBean(ElTransactionAspect.class)
    public ElTransactionAspect elTransactionAspect(ElTransactionTemplate elTransactionTemplate) {
        return new ElTransactionAspect(elTransactionTemplate);
    }

    @Override
    public void setEnvironment(Environment environment) {
        properties = Binder.get(environment)
                .bindOrCreate(
                        EmbeddedLuceneProperties.PREFIX,
                        EmbeddedLuceneProperties.class
                );
    }

}
